using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System;
 
public class BuildEditor : MonoBehaviour {
 
    private static string GameName = PlayerSettings.productName + "_" + DateTime.Now.ToString("yyyyMMddHHmm");
 
    private static string OutPutPath = System.IO.Directory.GetCurrentDirectory() + "\\Output\\";
    private static string ApkPath = string.Format("{0}{1}.apk", OutPutPath, GameName);
    private static string AndroidProjectPath = string.Format("{0}{1}_AndroidProject", OutPutPath, GameName);
    
 
    [MenuItem("Tools/打包/打包APK")]
    public static void BuildAPK()
    {
        string buildVersion = "1.0.0";
        int versionCode = 1;
        bool isDevelopment = false;
 
        Debug.Log("------------- receive comd para -------------");
        List<string> commondList = new List<string>();
        foreach (string arg in System.Environment.GetCommandLineArgs())
        {
            Debug.Log("cmd para--------------------------：" + arg);
            commondList.Add(arg);
        }
        try
        {
            Debug.Log("cmd para count -----------------------" + commondList.Count);
            buildVersion = commondList[commondList.Count - 3];
            versionCode = int.Parse(commondList[commondList.Count - 2]);
            isDevelopment = bool.Parse(commondList[commondList.Count - 1]);
        }
        catch (Exception)
        {
 
        }
 
        Debug.Log("------------- update resource -------------");
        //OneKeyRefreshSource.Create();
 
        Debug.Log("------------- start BuildAPK -------------");
        PlayerSettings.bundleVersion = buildVersion;
        PlayerSettings.Android.bundleVersionCode = versionCode;
        BuildOptions buildOption = BuildOptions.None;
        if (isDevelopment)
            buildOption |= BuildOptions.Development;
        else
            buildOption &= BuildOptions.Development;
        //PlayerSettings.Android.keystorePass = "00000000";       // 密钥密码
        //PlayerSettings.Android.keyaliasName = "test.keystore";    // 密钥别名
        //PlayerSettings.Android.keyaliasPass = "00000000";
        BuildPipeline.BuildPlayer(EditorBuildSettings.scenes, ApkPath, BuildTarget.Android, buildOption);
        Debug.Log("------------- end BuildAPK -------------");
        Debug.Log("Build path---------------------------：" + ApkPath);
        //Application.OpenURL(OutPutPath);
    }
}